package com.zxr.system.service.impl;

import java.util.Date;
import java.util.List;
import com.zxr.common.utils.DateUtils;
import com.zxr.system.domain.Member;
import com.zxr.system.domain.MemberProduct;
import com.zxr.system.service.IMemberProductService;
import com.zxr.system.service.IMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zxr.system.mapper.ProductsMapper;
import com.zxr.system.domain.Products;
import com.zxr.system.service.IProductsService;
import com.zxr.common.core.text.Convert;
import org.springframework.transaction.annotation.Transactional;

/**
 * 商品Service业务层处理
 * 
 * @author zxrproject
 * @date 2025-04-26
 */
@Service
public class ProductsServiceImpl implements IProductsService 
{
    @Autowired
    private ProductsMapper productsMapper;
    @Autowired
    private IMemberService memberService;
    @Autowired
    private IMemberProductService memberProductService;


    /**
     * 查询商品
     * 
     * @param productId 商品主键
     * @return 商品
     */
    @Override
    public Products selectProductsByProductId(Long productId)
    {
        return productsMapper.selectProductsByProductId(productId);
    }

    /**
     * 查询商品列表
     * 
     * @param products 商品
     * @return 商品
     */
    @Override
    public List<Products> selectProductsList(Products products)
    {
        return productsMapper.selectProductsList(products);
    }

    /**
     * 新增商品
     * 
     * @param products 商品
     * @return 结果
     */
    @Override
    @Transactional
    public int insertProducts(Products products)
    {
        products.setCreateTime(DateUtils.getNowDate());
        //新增商品
        productsMapper.insertProducts(products);
        //关联用户
        List<Member> members = memberService.selectMemberList(null);
        if(members.size()>0){
            for (int i=0;i<members.size();i++){
                MemberProduct memberProduct =new MemberProduct();
                memberProduct.setMemberId(members.get(i).getId());
                memberProduct.setProductId(products.getProductId());
                memberProduct.setmPrice(products.getPrice());
                memberProduct.setCreateTime(new Date());
                memberProductService.insertMemberProduct(memberProduct);
            }
        }
        return 1;
    }

    /**
     * 修改商品
     * 
     * @param products 商品
     * @return 结果
     */
    @Override
    public int updateProducts(Products products)
    {
        products.setUpdateTime(DateUtils.getNowDate());
        return productsMapper.updateProducts(products);
    }

    /**
     * 批量删除商品
     * 
     * @param productIds 需要删除的商品主键
     * @return 结果
     */
    @Override
    public int deleteProductsByProductIds(String productIds)
    {
        return productsMapper.deleteProductsByProductIds(Convert.toStrArray(productIds));
    }

    /**
     * 删除商品信息
     * 
     * @param productId 商品主键
     * @return 结果
     */
    @Override
    public int deleteProductsByProductId(Long productId)
    {
        memberProductService.deleteMemberProductByProductId(productId);
        return productsMapper.deleteProductsByProductId(productId);
    }
}
